import BaseService from './_BaseService'

class TaskTypeService extends BaseService {
  root = window.apiPath + '/task-types'

  search (query) {
    return super.search(query).then(list => {
      let l1 = list.filter(o => o.level === 1)
      let l2 = list.filter(o => o.level === 2)
      let l3 = list.filter(o => o.level === 3)
      l3.forEach(o3 => {
        let parent = l2.find(o2 => o2.id === o3.parentId)
        if (parent) {
          if (!parent.subset) {
            parent.subset = []
          }
          parent.subset.push(o3)
        }
      })

      l2.forEach(o2 => {
        let parent = l1.find(o1 => o1.id === o2.parentId)
        if (parent) {
          if (!parent.subset) {
            parent.subset = []
          }
          parent.subset.push(o2)
        }
      })

      return l1
    })
  }
  update (entity){
    return super.update(entity).then(res => {
      return res
    })
  }
  create (entity){
    return super.create(entity).then(res => {
      return res
    })
  }
  delete (id){
    return super.delete(id).then(res => {
      return res
    })
  }
}

export default new TaskTypeService()
